$(document).ready(function(){
		$("#ensurePrice").bind("input",function(){
			var val = $("#ensurePrice").val();
			if(isNaN(val))
			{
				$("#ensurePrice")[0].style.border = '1px solid red';
			}else
			{
				$("#ensurePrice")[0].style.border = '1px solid #04BE02';
			}
		});
		clearForm();
		checkPre();
		setValueByCookie();
		$("#token").val(new Date().getTime());
		//加载附近运营点
		loadNearbyOp();
	});

	function clearCookie()
	{
		$.cookie('order.jjdzId',null,{path:'/'});
		$.cookie('order.sjdzId',null,{path:'/'});
		$.cookie('order.operId',null,{path:'/'});
		$.cookie('order.expType',null,{path:'/'});
		$.cookie('order.ensurePrice',null,{path:'/'});
		$.cookie('order.weight',null,{path:'/'});
		$.cookie('order.wp',null,{path:'/'});
	}

	function loadNearbyOp()
	{
		getPointH5(function(data){
			$.ajax({
				url:'expressGetNearbyOper.do',
				async:false,
				data:
				{
					v:new Date().getTime()
				},
				success:function(ret)
				{
					var datas = ret.obj;
					for(var i=0;i<datas.length;i++){
						var dis = "";
						if(datas[i].distance >= 1)
						{
							dis = datas[i].distance.toFixed(2) + "km";
						}else
						{
							dis = datas[i].distance*1000 + "m";
						}
						$("#nearByDiv").append('<div class="weui_actionsheet_cell" id="jjd'+datas[i].opId+'"'+
									'data-mercno="'+datas[i].mercNo+'"'+
						        	'data-pubflag="'+datas[i].pubFlag+'"'+
						        	'data-mercname="'+datas[i].mercName+'"'+
						        	'data-address="'+datas[i].opAddress+'"'+
						        	'onclick="setJjd(\''+datas[i].opId+'\');"'+
								'>'+
								datas[i].opAddress+'<font color="gray" size="2px" style="margin-left:10px;">(距离：'+dis+')</font></div>');
					}
				},
				error:function()
				{
				}
			});
		},false,true);
	}
	
	function setValueByCookie()
	{
		var jjdzId = $.cookie('order.jjdzId');
		if(!isNull(jjdzId))
		{
			setDz(jjdzId,'jjdz');
		}
		var sjdzId = $.cookie('order.sjdzId');
		if(!isNull(sjdzId))
		{
			setDz(sjdzId,'sjdz');
		}
		var operId = $.cookie('order.operId');
		var expMercNo = $.cookie('order.expMercNo');
		if(!isNull(operId)) setJjd(operId,expMercNo);
		var ensurePrice = $.cookie('order.ensurePrice');
		if(!isNull(ensurePrice))
		{
			setEnsurePrice(ensurePrice);	
		}
		
		var weight = $.cookie('order.weight');
		var wp =$.cookie('order.wp'); 
		if(!isNull(weight) && !isNull(wp))
		{
			$("#ipt_weight").val(weight);
			$("#ipt_wp").val(wp);
			$("#wp_p").html("物品 <br><font size='2px'>"+wp+" &nbsp; "+weight+"kg");
		}
	}
	
	var checkRetData = null;
	function checkPre()
	{
		loading('正在检查数据,请稍候');
		$.ajax({
			url:'expressSendPre.do',
			async:false,
			data:
			{
				v:new Date().getTime()
			},
			success:function(ret)
			{
				unLoading();
				checkRetData = ret;
				checkRet();				
			},
			error:function()
			{
				unLoading();
				openToast('数据检查异常,这可能导致您提交订单失败');
			}
		});
	}
	
	
	function checkRet()
	{
		if(null == checkRetData)
		{
			return false;
		}
		if(checkRetData.code == '2')
		{
			confirm('提示','<font color="red">'+checkRetData.msg+'</font>',function(){
				to('companySearch.do');
			});
			return false;
		}else if(checkRetData.code == '1')
		{
			confirm('提示','<font color="red">'+checkRetData.msg+'</font>',function(){
				to('expressDetail.do?sn='+checkRetData.obj);
			});
			return false;
		}
		return true;
	}
	
	function openSheet(tag)
	{
		 var mask = $('#mask');
         var weuiActionsheet = $('#weui_actionsheet_'+tag);
         weuiActionsheet.addClass('weui_actionsheet_toggle');
         mask.show()
             .focus()//鍔爁ocus鏄负浜嗚Е鍙戜竴娆￠〉闈㈢殑閲嶆帓(reflow or layout thrashing),浣縨ask鐨則ransition鍔ㄧ敾寰椾互姝ｅ父瑙﹀彂
             .addClass('weui_fade_toggle').one('click', function () {
             hideActionSheet(weuiActionsheet, mask);
         });
         $('#actionsheet_cancel_'+tag).one('click', function () {
             hideActionSheet(weuiActionsheet, mask);
         });
         mask.unbind('transitionend').unbind('webkitTransitionEnd');

         $('.weui_actionsheet_cell').one('click', function () {
             hideActionSheet(weuiActionsheet, mask);
         });
         
         function hideActionSheet(weuiActionsheet, mask) {
             weuiActionsheet.removeClass('weui_actionsheet_toggle');
             mask.removeClass('weui_fade_toggle');
             mask.on('transitionend', function () {
                 mask.hide();
             }).on('webkitTransitionEnd', function () {
                 mask.hide();
             })
         }
	}
	
	function setDz(val,tab,isDefault)
	{
		if(!isDefault)
		{
			var key = 'order.'+tab+'Id';
			$.cookie(key,val,{expires:1,path:'/'});
		}
		
		var dataTag = '#dz'+val;
		if($(dataTag).length == 0)
		{
			return;
		}
		var name = $(dataTag).data('name');
		var phone = $(dataTag).data('phone');
		var adr = $(dataTag).data('adr');
		var a = "#ipt_adrId"+tab;
		$(a).val(val);
		$("#adr"+tab).html("<font size='2px'>已选择："+name+"("+phone+")"+"<br>"+adr+"</font>");
	}
	
	function setJjd(id,expType)
	{
		$.cookie('order.operId',id,{expires:1,path:'/'});
		$("#ipt_jjd").val(id);
		var dataTag = '#jjd'+id;
		if($(dataTag).length == 0)
		{
			return;
		}
		var mercNo   = $(dataTag).data("mercno");
		var pubFlag  = $(dataTag).data("pubflag");
		var mercName = $(dataTag).data("mercname");
		var address  = $(dataTag).data("address");
		
		$("#ipt_merNo").val(mercNo);
		var html = "<font  size='2px'>已选择："+address+"<br>";
			html += "所属企业："+mercName+"</font>";
		$("#jjd_p").html(html);
		if(null != pubFlag && pubFlag == '01')//对公
		{
			$("#publicFlagDiv")[0].style.display = '';
			$("#publicFlag").removeAttr("disabled","disabled");
		}else
		{
			$("#publicFlagDiv")[0].style.display = 'none';
			$("#publicFlag").attr("disabled","disabled");
		}
		
		$("#ipt_mercExpRelaId").val("");
		var html = "请选择快递公司";
		$("#expMerc_p").html(html);
		
		//先清空
		setExpressType();
		//根据商户号，运营点ID 去检索 快递公司
		getExpressTypes(mercNo,id,expType);
	}
	
	
	function getExpressTypes(mercNo,operId,expType)
	{
		$.ajax({
			url:'getExpressComps.do',
			async:true,
			data:{
				mercNo:mercNo,
				v:new Date().getTime(),
				operId:operId
			},
			success:function(data){
				$("#expTypeDiv").empty();
				if(data.code == '0')
				{
					var html = "";
					for(var i=0;i<data.obj.length;i++)
					{
						html += " <div id='exp"+data.obj[i].expMercNo+"'  class='weui_actionsheet_cell' "+
						" data-name='"+data.obj[i].expName+"' "+
						" data-mercname='"+data.obj[i].expMercName+"' "+
						" onclick='setExpressType(\""+data.obj[i].expMercNo+"\");' >"+data.obj[i].expName+"("+data.obj[i].expMercName+")"+"</div> ";
					}
					$("#expTypeDiv").html(html);
				}else
				{
					
				}
				if(expType)setExpressType(expType);
			},
			error:function(){
				
			}
		});
	}
	
	function setExpressType(expMercNo)
	{
		if(!expMercNo)
		{
			$("#ipt_expMercNo").val("");
			$("#expMerc_p").html("请选择快递公司");
		}
		$.cookie('order.expMercNo',expMercNo,{expires:1,path:'/'});
		var dataTag 	= '#exp'+expMercNo;
		if($(dataTag).length == 0)
		{
			return;
		}
		
		var expName 	= $(dataTag).data("name");
		var expMercName = $(dataTag).data("mercname");
		if(expMercNo && expName)
		{
			$("#ipt_expMercNo").val(expMercNo);
			var html = "<font size='2px'>已选择："+expName+"<br>快递分点："+expMercName+"</font>";
			$("#expMerc_p").html(html);
		}else
		{
			
			$("#ipt_expMercNo").val("");
			$("#expMerc_p").html("请选择快递公司");
		}
		
	}
	
	var _selWp;
	function setWp(str)
	{
		_selWp = str;
		show(".wgselfDiv");
		$("#wgInput").focus();
	}
	
	function setWg()
	{
		var val = $("#wgInput").val();
		if(isNaN(val))
		{
			return;
		}
		if(val <= 0)
		{
			return;
		}
		val = parseFloat(val);
		val = val.toFixed(2);
		$("#ipt_weight").val(val);
		$("#ipt_wp").val(_selWp);
		
		$.cookie('order.weight',val,{expires:1,path:'/'});
		$.cookie('order.wp',_selWp,{expires:1,path:'/'});
		
		$("#wp_p").html("物品 <br><font size='2px'>"+$("#ipt_wp").val()+" &nbsp; "+val+"kg </font>");
		hide('.wgselfDiv');
	}
	
	function viewService()
	{
		$(".dialogService")[0].style.display = 'block';
	}
	function closeService()
	{
		$(".dialogService")[0].style.display = 'none';
	}
	
	/*保价*/
	function openEnsurePrice()
	{
		$(".ensurePrice")[0].style.display = 'block';
		$("#ensurePrice").focus();
	}
	function setEnsurePrice(ensurePrice)
	{
		if(!ensurePrice)//来源于网页输入
		{
			ensurePrice = $("#ensurePrice").val();
			if(isNaN(ensurePrice))
			{
				return;
			}
			if(ensurePrice == '') ensurePrice = 0;
			ensurePrice = parseFloat(ensurePrice);
			if(ensurePrice < 0)
			{
				return;
			}
			$.cookie('order.ensurePrice',ensurePrice,{expires:1,path:'/'});
		}else//来源于cookie
		{
			ensurePrice = parseFloat(ensurePrice);
		}
		ensurePrice = ensurePrice.toFixed(2)
		$("#ensurePriceFont").html("保价 <br><font size='2px'>"+ensurePrice+"元</font>");
		$("#ipt_ensurePrice").val(ensurePrice);
		hide('.ensurePrice');
	}
	
	function setOther()
	{
		var other = $('#wpTypeInput').val();
		if(isNull(other))
		{
			$('#wpTypeInput').focus();
			return;
		}
		setWp(other);
		hide('.wpselfDiv');
	}

	function clearForm()
	{
		//$("#ipt_adrIdjjdz").val('');y由于设置了默认地址，所以不能将其清空
		$("#ipt_adrIdsjdz").val('');
		$("#ipt_jjd").val('');
		$("#ipt_mercNo").val('');
		$("#ipt_expMercNo").val('');
		$("#ipt_wp").val('');
		$("#ipt_weight").val('');
		$("#ipt_ensurePrice").val('');
	}
	
	/*校验订单 是否有效 并且提交订单*/
	function checkOrder()
	{
		if(!checkRet())
		{
			return;
		}
		
		var fromId 	= $("#ipt_adrIdjjdz").val();
		var direId 	= $("#ipt_adrIdsjdz").val();
		
		var jjd 	= $("#ipt_jjd").val();
		var mercNo 	= $("#ipt_mercNo").val();
		
		var expMercNo = $("#ipt_expMercNo").val();
		
		var publicFlag = $("#publicFlag").val();
		
		var wp = $("#ipt_wp").val();
		var weight = $("#ipt_weight").val();
		
		var ensurePrice = $("#ipt_ensurePrice").val();
		
		if(isNull(fromId))
		{
			openToast('请选择寄件地址',1000);
			return false;
		}
		if(isNull(direId))
		{
			openToast('请选择收件地址',1000);
			return false;
		}
		if(fromId == direId)
		{
			openToast('寄件地址和收件地址不能相同',1000);
			return false;
		}
		if(isNull(jjd))
		{
			openToast('请选择寄件点',1000);
			return false;
		}
		if(isNull(expMercNo))
		{
			openToast('请选择快递公司',1000);
			return false;
		}
		if(isNull(wp))
		{
			openToast('请选择物品',1000);
			return false;
		}
		if(isNull(weight))
		{
			openToast('请选择重量',1000);
			return false;
		}
		var xy = $("#xycheckbox").is(':checked');
		if(!xy)
		{
			openToast('请勾选《e快递服务协议》',1000);
			return false;
		}
		clearCookie();
		loading('正在提交...');
		$("#theform").submit();
	}